package tools.chapter25;

import java.util.concurrent.*;

/**
 * @author Kevin
 * @date 2020/4/14 11:27
 */
public class Enquiry {

    public static void main(String[] args) {

    }

    public static void useFuture() throws ExecutionException, InterruptedException {
        Integer r;
        // 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 异步向电商S1询价
        Future<Integer> f1 = executor.submit(()->getPriceByS1());
        // 异步向电商S2询价
        Future<Integer> f2 = executor.submit(()->getPriceByS2());
        // 异步向电商S3询价
        Future<Integer> f3 = executor.submit(()->getPriceByS3());

        // 获取电商S1报价并保存
        r = f1.get();
        Integer finalR1 = r;
        executor.execute(()->save(finalR1));
        // 获取电商S2报价并保存
        r = f2.get();
        Integer finalR2 = r;
        executor.execute(()->save(finalR2));
        // 获取电商S3报价并保存
        r = f3.get();
        Integer finalR3 = r;
        executor.execute(()->save(finalR3));
    }
    //增加阻塞队列，保证先获取到的报价先保存到数据库
    public static void useBlockQueue() throws InterruptedException {
        // 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 创建阻塞队列
        BlockingQueue<Integer> bq = new LinkedBlockingQueue<>();
        // 异步向电商S1询价
        Future<Integer> f1 = executor.submit(()->getPriceByS1());
        // 异步向电商S2询价
        Future<Integer> f2 = executor.submit(()->getPriceByS2());
        // 异步向电商S3询价
        Future<Integer> f3 = executor.submit(()->getPriceByS3());
        // 电商S1报价异步进入阻塞队列
        executor.execute(()-> {
            try {
                bq.put(f1.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
        // 电商S2报价异步进入阻塞队列
        executor.execute(()-> {
            try {
                bq.put(f2.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
        // 电商S3报价异步进入阻塞队列
        executor.execute(()-> {
            try {
                bq.put(f3.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
        // 异步保存所有报价
        for (int i = 0; i < 3; i++) {
            Integer r = bq.take();
            executor.execute(()->save(r));
        }
    }

    public static void useCompletionService() throws InterruptedException, ExecutionException {
        // 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 创建CompletionService
        CompletionService<Integer> cs = new ExecutorCompletionService<>(executor);
        // 异步向电商S1询价
        cs.submit(()->getPriceByS1());
        // 异步向电商S2询价
        cs.submit(()->getPriceByS2());
        // 异步向电商S3询价
        cs.submit(()->getPriceByS3());
        for (int i = 0; i < 3; i++) {
            Integer r = cs.take().get();
            executor.execute(()->save(r));
        }
    }


    public static Integer getPriceByS1()  throws InterruptedException {
        Thread.sleep(100);
        return 1;
    }
    public static Integer getPriceByS2()  throws InterruptedException {
        Thread.sleep(100);
        return 2;
    }
    public static Integer getPriceByS3()  throws InterruptedException {
        Thread.sleep(100);
        return 3;
    }
    public static void save(Integer in) {}
}
